<template>
  <a-modal
    title="故障列表"
    :width="1000"
    :visible="visible"
    :footer="null"
    @cancel="handleCancel"
  >
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">

            <a-col :md="!advanced && 6 || 24" :sm="24">
              <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                <a-button class="btn_edit" @click="() => {study()}"><a-icon type="reload" />学习</a-button>
              </span>
            </a-col>

          </a-row>
        </a-form>
      </div>
      <s-table
        ref="table"
        size="default"
        :columns="columns"
        :data="loadData"
        :alert="true"
        :rowKey="(record) => record.id"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      >

        <span slot="fileId" slot-scope="text,record">
          <!--        <a @click="$refs.playForm.play(record)">{{ text }}</a>-->
          <a @click="$refs.playForm.play(record)">试听</a>
        </span>
        <span slot="action" slot-scope="text, record">
          <!--        <a @click="$refs.editForm.edit(record)" class="btn_edit">编辑</a>-->

          <a-popconfirm placement="topRight" title="确认删除？" @confirm="() => del(record)">
            <a class="btn_del">删除</a>
          </a-popconfirm>
          <!--          <a-divider type="vertical"/>-->
          <!--          <a @click="look(record)" class="btn_details">查看</a>-->
        </span>
      </s-table>

      <!--      <add-form ref="addForm" @ok="handleOk"/>-->
      <!--      <edit-form ref="editForm" @ok="handleOk"/>-->
      <play-form ref="playForm" @ok="handleOk"/>
      <register-form ref="registerForm" @ok="handleOk"/>
    </a-card>
  </a-modal>
</template>

<script>
  import { STable } from '@/components'
  import { page, del } from '@/api/modular/main/bizexceptionlog/bizExceptionLogManage'
  import playForm from './playForm'
  import registerForm from './registerForm'

  export default {
    components: {
      STable,
      playForm,
      registerForm
    },

    data () {
      return {
        advanced: false, // 高级搜索 展开/关闭
        queryParam: {}, // 查询参数
        columns: [ // 表头
          {
            title: '故障文件',
            dataIndex: 'fileId',
            scopedSlots: { customRender: 'fileId' },
            align: 'center'
          },
          {
            title: '通道',
            dataIndex: 'gatewayChannel',
            align: 'center'
          },
          {
            title: '初次发生时间',
            dataIndex: 'createdAt',
            align: 'center'
          },
          {
            title: '故障发生时间',
            dataIndex: 'exceptionTime',
            align: 'center'
          },
          {
            title: '操作',
            dataIndex: 'action',
            scopedSlots: { customRender: 'action' },
            align: 'center'
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return page(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        },
        selectedRowKeys: [],
        selectedRows: [],
        defaultExpandedKeys: [],
        replaceFields: {
          key: 'id'
        },
        visible: false,
        index: -1,
        url: window.CONST.VUE_APP_API_BASE_URL,
        deviceId: ''
      }
    },

    methods: {
      // 打开此页面首先加载此方法
      toIndex (record) {
        const that = this
        that.visible = true
        that.deviceId = record.deviceId
        that.queryParam.exceptionId = record.exceptionId
        that.queryParam.deviceId = record.deviceId
        that.queryParam.createdAt = record.createdAt
        try {
          that.$refs.table.refresh()
        } catch (e) {
          // 首次进入界面，因表格加载顺序，会抛异常，我们不予理会
        }
      },
      del (record) {
        del(record).then((res) => {
          if (res.success) {
            this.$message.success('删除成功')
            this.$refs.table.refresh()
          } else {
            this.$message.error('删除失败：' + res.message)
          }
        }).catch((err) => {
          this.$message.error('删除错误：' + err.message)
        })
      },
      look (record) {
        window.open(this.url + '/admin/index1.html#/analysis?id=' + record.id)
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      handleOk () {
        this.$refs.table.refresh()
      },
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      },
      handleCancel () {
        this.queryParam = {}
        this.visible = false
      },
      study () {
        var fileNames = []
        for (let i = 0; i < this.selectedRows.length; i++) {
          fileNames.push(this.selectedRows[i].fileName)
        }
        if (fileNames.length >= 3) { this.$refs.registerForm.toRegister(fileNames.toString()) } else { this.$message.warn('至少选择三个样本文件') }
      }
    }
  }
</script>

<style lang="less">
.table-operator {
  margin-bottom: 18px;
}
button {
  margin-right: 8px;
}
</style>
